package cn.ljy.mes.web.controller.backstage.mes.productionorder;


import cn.ljy.authority.web.controller.backstage.BaseBackstageController;
import cn.ljy.common.annotation.LimitRequest;
import cn.ljy.common.model.result.ResponseData;
import cn.ljy.mes.model.MesProductionOrder;
import cn.ljy.mes.model.query.MesProductionOrderQuery;
import cn.ljy.mes.service.MesProductionOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;

@Api(tags = "后台生产订单管理模块")
@RestController
@RequestMapping(value = "/backstage/mes/productionOrder")
public class MesProductionOrderController extends BaseBackstageController<MesProductionOrder, MesProductionOrderQuery,String> {

    @Autowired
    private MesProductionOrderService baseService;

    @Override
    protected String getAuthorityPrefix() {
        return "mes:productionOrder";// 定义模块权限前缀
    }



    @ApiOperation(value = "将已完成的商城订单转为生产订单")
    @PreAuthorize("hasAuthority('mes:productionOrder:manage')")
    @LimitRequest
    @PostMapping("/generate")
    public ResponseData generateProductionOrder() {
        return ResponseData.success(baseService.createProductionOrdersByShopCarOrder());
    }

    @ApiOperation(value = "开始生产（将订单转为生产状态")
    @PreAuthorize("hasAuthority('mes:productionOrder:manage')")
    @LimitRequest
    @ApiImplicitParam(name = "ids", value = "要删除的id集合", required = true,paramType = "query",example ="[11,12,13]")
    @PatchMapping("/beginProduction")
    public ResponseData beginProduction(@NotEmpty(message="请选择记录")@RequestBody String[] ids) {
        return ResponseData.success(baseService.beginProduction(ids));
    }

    @ApiOperation(value = "订单状态管理",notes = "只能是对paused、cancelled状态进行修改，其他状态会有相应的函数和模块完成")
    @PreAuthorize("hasAuthority('mes:productionOrder:manage')")
    @LimitRequest
    @PatchMapping("/{orderId}/status")
    public ResponseData updateOrderStatus(@PathVariable String orderId,@NotBlank(message = "订单状态不能为空") String targetStatus) {
        baseService.changeProductionStatus(orderId, targetStatus);
        return ResponseData.success(null);
    }

    @ApiOperation(value = "整车审核")
    @PreAuthorize("hasAuthority('mes:productionOrder:manage')")
    @LimitRequest
    @ApiImplicitParam(name = "ids", value = "要删除的id集合", required = true,paramType = "query",example ="[11,12,13]")
    @PatchMapping("/inspection")
    public ResponseData inspectionProductionOrders(@RequestBody MesProductionOrder productionOrder) {
        baseService.inspectionProductionOrders(productionOrder.getId(), productionOrder.getAuditResult(), productionOrder.getAuditNote());
        return ResponseData.success(null);
    }
}